package com.zscat.mallplus.controller.order;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.entity.vo.sms.CouponListInfo;
import com.zscat.mallplus.entity.vo.sms.CouponResponse;
import com.zscat.mallplus.service.sms.service.ISmsCouponHistoryService;
import com.zscat.mallplus.service.sms.service.ISmsCouponService;
import com.zscat.mallplus.service.ums.service.IUmsMemberService;
import com.zscat.mallplus.sms.entity.SmsCoupon;
import com.zscat.mallplus.sms.entity.SmsCouponHistory;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.util.DateUtil;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.vo.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 我的个人中心-用户优惠券
 * https://github.com/shenzhuan/mallplus on 2018/8/29.
 */
@RestController
@RequestMapping("/api/userCoupon")
public class MemberCouponController {

    @Autowired
    private ISmsCouponService couponService;

    @Autowired
    private IUmsMemberService memberService;

    @Autowired
    private ISmsCouponHistoryService smsCouponHistoryService;

    /**
     * 获取用户领取的优惠券列表
     *
     * @param useStatus 优惠券筛选类型:0->未使用；1->已使用；2->已过期
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(@RequestParam(value = "useStatus", required = false) Integer useStatus,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex) {
        UmsMember umsMember = memberService.getNewCurrentMember();
        List<SmsCouponHistory> couponHistoryList = smsCouponHistoryService.page(new Page<>(pageIndex, pageSize), new QueryWrapper<SmsCouponHistory>()
                .eq("use_status", useStatus).
                        eq("member_id", umsMember.getId()).
                        eq("is_delete", 1)).getRecords();
        System.out.println("用户id:" + umsMember.getId());
        List<CouponListInfo> recordList = new ArrayList<>();
        for (SmsCouponHistory smsCouponHistory : couponHistoryList) {
            SmsCoupon smsCoupon = couponService.getById(smsCouponHistory.getCouponId());
            if (smsCoupon == null) {
                continue;
            }
            CouponListInfo couponListInfo = new CouponListInfo();
            couponListInfo.setUseStatus(useStatus);
            couponListInfo.setUseType(smsCoupon.getUseType());
            couponListInfo.setStartTime(DateUtil.dateToStr(smsCoupon.getStartTime(), "yyyy-MM-dd"));
            couponListInfo.setEndTime(DateUtil.dateToStr(smsCoupon.getStartTime(), "yyyy-MM-dd"));
            couponListInfo.setNote("满" + smsCoupon.getMinPoint() + "元减" + smsCoupon.getAmount());
            couponListInfo.setId(smsCouponHistory.getId());
            couponListInfo.setAmount(smsCoupon.getAmount());
            recordList.add(couponListInfo);
        }
        PageInfo<CouponListInfo> page = new PageInfo<>(pageIndex, pageSize, recordList);
        return new CommonResult().success(page);
    }

    /**
     * 删除领取的优惠券
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object deleteCoupon(Long[] ids) {
        // 删除
        for (Long id : ids) {
            SmsCouponHistory smsCouponHistory = smsCouponHistoryService.getById(id);
            smsCouponHistory.setIsDelete(0);
            smsCouponHistoryService.updateById(smsCouponHistory);
        }
        return new CommonResult().success();
    }

}
